package com.hu.zhcc.shiro.service.xsf.impl;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.common.entity.PageRequest;
import com.hu.zhcc.shiro.dao.xsf.THealthPlanMapper;
import com.hu.zhcc.shiro.entity.xsf.dto.TEvaluateDTO;
import com.hu.zhcc.shiro.entity.xsf.dto.THealthPlanDTO;
import com.hu.zhcc.shiro.entity.xsf.pojo.TEvaluate;
import com.hu.zhcc.shiro.entity.xsf.pojo.THealthPlan;
import com.hu.zhcc.shiro.service.xsf.THealthPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class THealthPlanImpl implements THealthPlanService {
    @Autowired
    private THealthPlanMapper tHealthPlanMapper;

    @Override
    public Page<THealthPlanDTO> queryAllTHealthPlan(Map<String, Object> paremeters, int offset, int limit) {
        int total = this.tHealthPlanMapper.countTHealthPlan(paremeters);
        List<THealthPlanDTO> dtoList = null;
        if(total > 0) {
            PageRequest pageRequest = new PageRequest(offset, limit, paremeters, null);
            List<THealthPlan> list = this.tHealthPlanMapper.queryAllTHealthPlan(pageRequest);
            dtoList = new ArrayList<THealthPlanDTO>(list.size());
            for(THealthPlan tHealthPlan : list) {
                THealthPlanDTO dto = new THealthPlanDTO();
                BeanUtils.copyProperties(tHealthPlan, dto);
                dtoList.add(dto);
            }
        }
        return new Page<THealthPlanDTO>(total, dtoList);
    }

    @Override
    public THealthPlanDTO getById(int id) {
        THealthPlan tHealthPlan = this.tHealthPlanMapper.selectByPrimaryKey(id);
        if(tHealthPlan == null) {
            return null;
        }
        THealthPlanDTO dto = new THealthPlanDTO();
        BeanUtils.copyProperties(tHealthPlan, dto);
        return dto;
    }

    @Override
    public THealthPlanDTO saveTHealthPlan(THealthPlanDTO dto) {
        THealthPlan tHealthPlan = new THealthPlan();
        BeanUtils.copyProperties(dto, tHealthPlan);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        tHealthPlan.setCreateTime(simpleDateFormat.format(new Date()));
        this.tHealthPlanMapper.insert(tHealthPlan);
        dto.setId(tHealthPlan.getId());
        return dto;
    }

    @Override
    public int removeTHealthPlan(int id) {
        return tHealthPlanMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateTHealthPlan(THealthPlan tHealthPlan) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        tHealthPlan.setUpdateTime(simpleDateFormat.format(new Date()));
        return tHealthPlanMapper.updateByPrimaryKeySelective(tHealthPlan);

    }
}
